<?php

namespace App\Admin\Controllers;

use App\Models\Books;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BookController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Models\Books';

    private static $overType = [
        '1'=>'连载',
        '2'=>'完结',
    ];

    private static $status = [
        '1'=>'显示',
        '0'=>'隐藏',
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Books);

        $grid->model()->orderBy('sort', 'desc');
        $grid->model()->take(20);

        $grid->column('id', __('小说ID'));
        $grid->column('name', __('小说名称'));
        $grid->column('image', __('封面图'))->image('',80,100);
        $grid->column('type', __('分类'));
        $grid->column('writer', __('作者'));
        $grid->column('intro', __('简介'))->limit(50)->hide();
        $grid->column('is_over', __('连载状态'));
        /*$grid->column('is_over', __('连载状态'))->display(function ($isOver) {
            return self::$overType[$isOver];
        });*/
        $grid->column('click_num', __('点击量'));
        $grid->column('sort', __('排序'))->sortable();
        $grid->column('status', __('状态'))->display(function ($status) {
            return self::$status[$status];
        });
        $grid->column('new_chapter', __('最新章节'));
        $grid->column('created_at', __('创建时间'))->hide();
        $grid->column('updated_at', __('更新时间'));

        // filter($callback)方法用来设置表格的简单搜索框
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 设置created_at字段的范围查询
            $filter->between('updated_at', '更新时间')->datetime();
            // 小说名称/作者
            $filter->where(function ($query) {
                $query->where('name', 'like', "%{$this->input}%")
                    ->orWhere('writer', 'like', "%{$this->input}%");
            }, '小说名称/作者');
        });

        // 禁用删除按钮

        //禁用创建按钮
        $grid->disableCreateButton();
        //禁用行选择器
        $grid->disableRowSelector();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Books::findOrFail($id));

        $show->field('id', __('小说ID'));
        $show->field('name', __('小说名称'));
        $show->field('image', __('封面图'))->image();
        $show->field('type', __('类型'));
        $show->field('writer', __('作者'));
        $show->field('intro', __('简介'));
        $show->field('is_over', __('连载状态'));
        $show->field('click_num', __('点击量'));
        $show->field('sort', __('排序'));
        $show->field('status', __('状态'))->using(self::$status);
        $show->field('new_chapter', __('最新章节'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     *
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Books);

        $form->display('id', __('小说ID'));
        $form->display('name', __('小说名称'));
        $form->image('image', __('封面图'));
        $form->display('type', __('类型'));
        $form->text('writer', __('作者'));
        $form->text('intro', __('简介'));
        $form->radio('is_over', __('连载状态'))->options(self::$overType);
        $form->number('click_num', __('点击量'));
        $form->number('sort', __('排序'));
        $form->switch('status', __('状态'));
        $form->display('new_chapter', __('最新章节'));
        $form->display('updated_at', __('更新时间'));

        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
        });

        //保存后回调
        $form->saved(function (Form $form) {
            //删除版本缓存
            Books::updateBookCache($form->model()->id);
        });

        return $form;
    }

}
